package org.ddr.img;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class passmaker
 */
public class passmaker extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public passmaker() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("image/jpeg");
		BufferedImage img = new BufferedImage(150, 50, BufferedImage.TYPE_INT_RGB);
		
		OutputStream out = response.getOutputStream();
		
		Graphics g = img.getGraphics();
		
		g.setColor(Color.red);
		g.fillRect(0, 0, 150, 50);
		
		g.setColor(Color.black);
		
		Font f = new Font("Hevetica", Font.BOLD + Font.ITALIC, 20);
		g.setFont(f);
		
		int bj1 = (int)((Math.random()*100) + 1);
		char bj2 = (char)((Math.random()*25) + 65);
		int bj3 = (int)((Math.random()*100) + 1);
		char bj4 = (char)((Math.random()*25) + 65);
		
		String str = "" + bj1 + bj2 + bj3 + bj4;
		HttpSession session = request.getSession();
		session.setAttribute("str", str);
		
		g.drawString(str, 40, 30);
		
		ImageIO.write(img, "jpg", out);
	}

}
